package com.qiangqiang.task.server.service;

import com.qiangqiang.task.server.dto.FollowDTO;
import com.qiangqiang.task.server.entity.Comments;
import com.qiangqiang.task.server.entity.Follow;
import com.qiangqiang.task.server.mapper.FollowMapper;
import com.qiangqiang.task.server.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2018-12-31 14:34
 **/
@Service
public class FollowService extends BaseService{

    SnowflakeIdWorker commentsWorker = new SnowflakeIdWorker(0, 5);

    @Autowired
    FollowMapper followMapper;

    public boolean addFollowRecord(Follow follow) {
        follow.setFollowId(commentsWorker.nextId());
        follow.setCreateTime(Instant.now().toEpochMilli());
        follow.setUpdateTime(Instant.now().toEpochMilli());
        return followMapper.insertFollow(follow) > 0;
    }

    public List<FollowDTO> findAllFollow(long userId) {
        return followMapper.queryFollowsOrFans(userId, false).stream()
                .map(e->beanToDTO(e.getFollowedUserId())).collect(Collectors.toList());
    }

    public List<FollowDTO> findAllFans(long followerId){
        return followMapper.queryFollowsOrFans(followerId,true).stream()
                .map(e->beanToDTO(e.getUserId())).collect(Collectors.toList());
    }

    /**
     * @Description: 判断用户是否关注了TA
     * @Param:
     * @return: true：没关注，可以执行操作，false：关注了，不能往下执行
     * @Author: sky.lin
     * @Date: 2018/12/31
     */
    public boolean isFollowed(long userId, long followerId) {
        return followMapper.isFollowed(userId, followerId) == null;
    }
}
